package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.bas.entity.BasRoomState;
import com.kingint.mbg.bas.entity.BasWarrantyItems;
import com.kingint.mbg.bas.mapper.BasRoomStateMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasRoomStateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.MenuUtil;
import com.kingint.web.util.tree.TreeBo;
import com.kingint.web.util.tree.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 客房状态相关表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-21
 */
@Service
@Slf4j
public class BasRoomStateServiceImpl extends ServiceImpl<BasRoomStateMapper, BasRoomState> implements BasRoomStateService {

    @Resource
    private UmsAdminService adminService;
    @Resource
    private BasRoomStateMapper basRoomStateMapper;

    @Override
    public int insertBasRoomStateInfo(BasRoomState basRoomState) {
        UmsMember currentMember = adminService.getCurrentMember();
        basRoomState.setCreateTime(new Date());
        basRoomState.setHotelId(currentMember.getHotelId());
        int insert = basRoomStateMapper.insert(basRoomState);
        return insert;
    }

    @Override
    public int updateRoomStateInfo(BasRoomState basRoomState) {
        int i = basRoomStateMapper.updateById(basRoomState);
        return i;
    }

    @Override
    public int deleteRoomStateInfo(Integer id) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        QueryWrapper<BasRoomState> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",id).eq("hotel_id", hotelId);
        int count = this.count(queryWrapper);
        if(count>0){
            return -1;
        }
        BasRoomState roomState = new BasRoomState();
        roomState.setId(id);
        roomState.setStatus(1);//删除
        int i = basRoomStateMapper.updateById(roomState);
        return i;
    }

    @Override
    public Object getParentBasRoomState(Integer pageNum, Integer pageSize, BasRoomState basRoomState) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("对应账号没有关联酒店id");
            return null;
        }
//        IPage<BasRoomState> page = new Page<>(pageNum, pageSize);
//        QueryWrapper<BasRoomState> wrapper = new QueryWrapper<>();
//        wrapper.eq("hotel_id",currentMember.getHotelId())
//                .eq("status",0)
//                .eq("pid",0);
//        IPage<BasRoomState> iPage = basRoomStateMapper.selectPage(page, wrapper);
//        List<BasRoomState> records = iPage.getRecords();//父类菜单
//
//        QueryWrapper<BasRoomState> wrapper1 = new QueryWrapper<>();//查询所有的信息
//        wrapper1.eq("hotel_id",currentMember.getHotelId())
//                .eq("status",0);
//        List<BasRoomState> basRoomStates = basRoomStateMapper.selectList(wrapper1);
//        MenuUtil.cancelRoomState(records,basRoomStates);
//        return iPage;
        QueryWrapper<BasRoomState> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("status", 0);
        List<BasRoomState> list = this.list(wrapper);
        TreeUtil<BasRoomState> basGoodsTreeUtil = new TreeUtil<BasRoomState>("getId","getPid");

        List<TreeBo<BasRoomState>> createTreeAsTreeEntity=null;
        List<BasRoomState> list2=new ArrayList<>();
        try {

            createTreeAsTreeEntity = basGoodsTreeUtil.createTreeAsTreeEntity(new HashSet<BasRoomState>(list));
            if(createTreeAsTreeEntity.size()>0)
                tree(list2,createTreeAsTreeEntity);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return K.ok().data(list2);
    }
    private void  tree(List<BasRoomState> list,List<TreeBo<BasRoomState>>  TreeBo){
        for (TreeBo<BasRoomState> itemsTreeBo : TreeBo) {
            BasRoomState title = itemsTreeBo.getTitle();
            List<TreeBo<BasRoomState>> children = itemsTreeBo.getChildren();
            if(children!=null){
                tree(title.getChildren(),children);
            }
            list.add(title);
        }

    }
}
